import logging
import requests
from api.login_api import Mepsking_Login
from api.search_goods_api import Search_Goods
from config import Base_Host
class Goods:
    def buy_goods(self,spuCode,token):
        url=Base_Host+'/api/product/goodsShow/getGoodsSkuShowList?spuCodes='+spuCode
        header_dict={'accessToken':token}
        resp=requests.get(url=url,headers=header_dict)
        logging.info('购买接口返回的数据为：{}'.format(resp.json()))
        return [resp,token]
    def couponId(self,json_dict,spuCode,token):
        url=Base_Host+'/api/coupon/couponDistribution/getList'
        resp=self.buy_goods(spuCode,token)
        token=resp[1]
        header_dict = {'accessToken': token}
        resp=requests.post(url=url,json=json_dict,headers=header_dict)
        coupId=resp.json().get('result')[0].get("distributionId")
        logging.info('获取到的coupId为：{}'.format(coupId))
    def spuId(self,spuCode,token):
        resp=self.buy_goods(spuCode,token)
        spuId01=resp[0].json().get('result')
        spuId02=spuId01[0].get('goodsSkusCartVoList')
        spuId=spuId02[0].get('spuId')
        weight=spuId02[0].get('weight')
        logging.info('获取到的spuId,weight为：{},{}'.format(spuId,weight))
        return [spuId,weight]
    def coupon(self,spuCode,token):
        resp=self.spuId(spuCode,token)
        spuId=resp[0]
        resp=self.buy_goods(spuCode,token)
        token=resp[1]
        header_dict = {'accessToken': token}
        url=Base_Host+'/api/coupon/shopCoupon/getCouponListBySpuId?spuId='+spuId
        resp=requests.get(url=url,headers=header_dict)
        coupon=resp.json().get('result')[0].get('category')
        logging.info('优惠券接口返回的数据为：{}'.format(coupon))
        return coupon
    def AddressList(self,token):
        url=Base_Host+'/api/system/shopCustomerAddress/getMyAddressList'
        header_dict = {'accessToken': token}
        resp=requests.get(url=url,headers=header_dict)
        logging.info('地址接口返回的数据为：{}'.format(resp.json()))
        return resp
if __name__ == '__main__':
    resp=Mepsking_Login()
    json_dict = {"account": "2524726232@qq.com", "password": "8c730e7f3dac741443cd3a57cdb7fd8f", "sourceFrom": "PC"}
    token=resp.token(json_dict)
    # goods=Search_Goods()
    # spucode=goods.spucode('私奔乐器机翼',token)
    test=Goods()
    resp=test.AddressList(token)
    # resp=test.spuId("221010857654",token)
    # resp=spucode.cityId(token)
    # json_dict={"orderAmount": "40.00","goodsInfo": [{"spuId": "1574664271236796417", "skuId": "1628972386198597634", "qty": 1, "price": 40}]}
    # resp = test.couponId(json_dict,spucode, token)


